<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Customer_region_model short summary.
 *
 * Customer_region_model description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Customer_region_model extends CI_Model
{
    public $customer_region_id;
    public $c_name;
    public $phone;
    public $tel;
    public $province;
    public $city;
    public $county;
    public $detailed_add;
    public $customer_id;
    public $is_default;
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    public function inset_customer_region($customer_regionModel)
    {
        return $this->db->insert('customer_region',$customer_regionModel);
    }

    /**
     * 添加客户地址并更新订单默认地址
     * @param mixed $customer_regionModel 客户地址模型
     * @param mixed $goods_order_id 订单id
     * @return int
     */
    public function inset_customer_region_from_order($customer_regionModel,$goods_order_id)
    {
        $this->db->trans_begin();
        $this->db->insert('customer_region',$customer_regionModel);
        //如果添加的地址为默认地址，则更新对应订单邮寄地址id
        if($customer_regionModel->is_default==1)
        {
            $customer_region_insertId=$this->db->insert_id();
            $sql_update_order_region="update goods_order set customer_region_id=$customer_region_insertId where is_del=0 and goods_order_id=$goods_order_id";
            $this->db->query($sql_update_order_region);
        }

        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }
    public function get_default_by_customer($customer_id)
    {
        $sql="select * from customer_region where is_del=0 and customer_id=$customer_id and is_default=1";
        return $this->db->query($sql)->row();
    }

    public function update_customer_region($customer_regionModel)
    {
        $sql="update customer_region set c_name='".$customer_regionModel->c_name."',phone='".$customer_regionModel->phone."',tel='".$customer_regionModel->tel."',province='".$customer_regionModel->province."',city='".$customer_regionModel->city."',county='".$customer_regionModel->county."',detailed_add='".$customer_regionModel->detailed_add."' where is_del=0 and customer_region_id=".$customer_regionModel->customer_region_id;
        return $this->db->query($sql);
    }

    public function get_customer_region_by_customer_id($customer_id)
    {
        $sql="select * from customer_region where is_del=0 and customer_id=$customer_id order by is_default desc";
        return $this->db->query($sql)->result();
    }

    public function get_region_by_id($customer_region_id)
    {
        $sql="select * from customer_region where is_del=0 and customer_region_id=$customer_region_id";
        return $this->db->query($sql)->row();
    }

    /**
     * 设置客户默认邮寄地址
     * @param mixed customer_region_id  邮寄地址id
     * @return int 返回更新状态值
     */
    public function set_default($customer_region_id)
    {
        $old_default_sql="select customer_region_id from customer_region where is_del=0 and is_default=1";
        $old_default_models=$this->db->query($old_default_sql)->result();
        $this->db->trans_begin();
        if(count($old_default_models)>0)
        {
            foreach($old_default_models as $old_default_item)
            {
                $undefault_sql="update customer_region set is_default=0 where is_del=0 and customer_region_id=".$old_default_item->customer_region_id;
                $this->db->query($undefault_sql);
            }
        }
        $setdefault_sql="update customer_region set is_default=1 where is_del=0 and customer_region_id=".$customer_region_id;
        $this->db->query($setdefault_sql);
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }

    /**
     * Summary of 删除客户邮寄地址
     * @param mixed region_id 客户邮寄地址Id
     */
    public function delete_customer_region($customer_region_id)
    {
        $customer_region_model=self::get_region_by_id($customer_region_id);
        $this->db->trans_begin();
        //更新客户地址为删除状态，并更改默认值为0
        $sql_delete_region="update customer_region set is_del=1,is_default=0  where customer_region_id=$customer_region_id";
        $this->db->query($sql_delete_region);
        if($customer_region_model->is_default==1)
        {

            //如果删除的地址为默认地址，则设置地址中第一条记录为默认
            $update_first_default_sql="update customer_region set is_default=1 where is_del=0 and customer_id=$customer_region_model->customer_id  ORDER BY customer_region_id ASC limit 1";
            $this->db->query($update_first_default_sql);
        }
        //如删除的客户地址在用户未支付订单中出现，则更新该订单邮寄地址
        $update_order_sql="update goods_order
                           LEFT JOIN customer_region
                           on customer_region.customer_id=goods_order.customer_id and customer_region.is_del=0 and customer_region.is_default=1
                           set goods_order.customer_region_id= customer_region.customer_region_id
                           where goods_order.customer_id=$customer_region_model->customer_id and goods_order.order_state=0 and goods_order.customer_region_id=$customer_region_id";
        $this->db->query($update_order_sql);
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }
}